package com.kingdee.eas.nbport.jy.app;

import java.util.Set;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectStringPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.bot.BOTRelationInfo;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.base.botp.BOTBillOperStateEnum;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fi.gl.VoucherFactory;
import com.kingdee.eas.fi.gl.VoucherInfo;
import com.kingdee.eas.nbport.jy.IncomeCategoryInfo;
import com.kingdee.eas.nbport.jy.InvoiceCollection;
import com.kingdee.eas.nbport.jy.InvoiceFactory;
import com.kingdee.eas.nbport.jy.InvoiceInfo;

public class InvoiceControllerBean extends AbstractInvoiceControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.nbport.jy.app.InvoiceControllerBean");

	protected void _updateIncomeCategory(Context ctx, Set ids, IObjectValue type)
			throws BOSException, EASBizException {
		// super._updateIncomeCategory(ctx, ids, type);
		// String sql = "update T_JY_Invoice set "
		EntityViewInfo evi = new EntityViewInfo();
		FilterInfo f = new FilterInfo();
		f.getFilterItems().add(
				new FilterItemInfo("id", ids, CompareType.INCLUDE));
		evi.setFilter(f);

		InvoiceCollection col = InvoiceFactory.getLocalInstance(ctx)
				.getInvoiceCollection(evi);
		for (int i = 0; i < col.size(); i++) {
			InvoiceInfo invoiceInfo = col.get(i);
			invoiceInfo.setIncomeCategory((IncomeCategoryInfo) type);
			InvoiceFactory.getLocalInstance(ctx).update(
					new ObjectUuidPK(invoiceInfo.getId()), invoiceInfo);
		}
		
	}
	protected void _reverseSave(Context ctx, IObjectPK srcBillPK,
			IObjectValue srcBillVO, BOTBillOperStateEnum bOTBillOperStateEnum,
			IObjectValue bOTRelationInfo) throws BOSException, EASBizException {
		BOTRelationInfo relation = (BOTRelationInfo) bOTRelationInfo;
		BOSObjectType bosType = BOSUuid.read(relation.getDestObjectID())
				.getType();
		if (bosType.equals((new VoucherInfo()).getBOSType())) {
			InvoiceInfo info = (InvoiceInfo) srcBillVO;
			if (bOTBillOperStateEnum.equals(BOTBillOperStateEnum.DELETE)) {

				info.setFivouchered(false);
				info.setVoucherNumber(null);
			} else {
				String voucherID = relation.getDestObjectID();
				VoucherInfo voucherInfo = (VoucherInfo) VoucherFactory
						.getLocalInstance(ctx).getValue(
								new ObjectStringPK(voucherID));
				info.setFivouchered(true);
				info.setVoucherNumber(voucherInfo.getNumber());

			}
		}
		super._reverseSave(ctx, srcBillPK, srcBillVO, bOTBillOperStateEnum,
				bOTRelationInfo);
	}

}